from DataLayer import ConnDBAndCloseDB
from DataLayer import UserDataLoad


# 登录验证
def login(account,pwd):
    # account:账号
    # pwd:密码
    # 返回值，不存在该account则返回-1，密码错误则返回0，顾客则返会Customer对象，客服员则返回Waiter对象，店主则返回Host对象
    conn,cur=ConnDBAndCloseDB.connectDB()
    # 先查找该账号，不存在该账号则返回-1
    user=UserDataLoad.selUserBasicByAccount(cur,account)
    if not user:
        return -1
    else:
        # 若存在该账号，则验证密码，若密码错误，则返回0
        user=UserDataLoad.selUserBasicByAccAndPwd(cur, account, pwd)
        ConnDBAndCloseDB.closeDB(cur,conn)
        if not user:
            return 0
        else:
            return user.type

# 注册
def register(account,pwd,pwdAffirm):
    # account 账号
    # pwd密码
    # pwdAffirm确认密码
    # 返回值：-1代表账号已被注册，0代表密码与确认密码不匹配，1代表注册成功
    conn, cur = ConnDBAndCloseDB.connectDB()
    user = UserDataLoad.selUserBasicByAccount(cur, account)
    if not user:
        # 此账号未被注册
        # 进行密码确认
        if pwd==pwdAffirm:
            # 向数据库里添加内容
            UserDataLoad.addCustomer(conn,cur,account,pwd,"个人地址","../photodefualt.png")
            ConnDBAndCloseDB.closeDB(cur, conn)
            return 1
        else:
            # 密码与确认密码不匹配
            ConnDBAndCloseDB.closeDB(cur, conn)
            return 0
    else:
        # 此账号已被注册
        ConnDBAndCloseDB.closeDB(cur, conn)
        return -1

